/*
 * Copyright 2007 Agile Plugins Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.agileplugins.cruisewatcher;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.ApplicationComponent;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.JDOMExternalizable;
import com.intellij.openapi.util.WriteExternalException;
import org.jdom.Element;

import javax.swing.*;

public class CruiseWatcherPlugin implements ApplicationComponent, JDOMExternalizable, Configurable {
    private AllBuildsWatcher allBuildsWatcher;
    private CruiseWatcherSettings settings = new CruiseWatcherSettings();
    private CruiseWatcherConfigurationPanel configurationPanel;

    public String getComponentName() {
        return "CruiseWatcherPlugin";
    }

    public static CruiseWatcherPlugin getInstance() {
        return ApplicationManager.getApplication().getComponent(CruiseWatcherPlugin.class);
    }

    public void initComponent() {
        allBuildsWatcher = new AllBuildsWatcher(settings);
        allBuildsWatcher.start();
    }

    public void disposeComponent() {
        allBuildsWatcher.stopRunning();
    }

    public void readExternal(Element element) throws InvalidDataException {
        settings.readExternal(element);
    }

    public void writeExternal(Element element) throws WriteExternalException {
        settings.writeExternal(element);
    }

    public String getDisplayName() {
        return "CruiseWatcher";
    }

    public Icon getIcon() {
        return UIManager.getIcon("OptionPane.questionIcon");
    }

    public String getHelpTopic() {
        return null;
    }

    public JComponent createComponent() {
        configurationPanel = new CruiseWatcherConfigurationPanel(settings);
        return configurationPanel;
    }

    public boolean isModified() {
        return configurationPanel.isModified();
    }

    public void apply() throws ConfigurationException {
        configurationPanel.apply();
        allBuildsWatcher.reset();
    }

    public void reset() {
        configurationPanel.reset();
    }

    public void disposeUIResources() {
        configurationPanel = null;
    }

    public CruiseWatcherSettings getSettings() {
        return settings;
    }

    public void pause() {
        allBuildsWatcher.pauseDisplay();
    }

    public void resume() {
        allBuildsWatcher.resumeDisplay();
    }
}
